In Java, the "for" loop is an entry-controlled loop that enables users to run a block of statements repeatedly for a predetermined number of times. The test-condition specified inside the "for" loop determines the
number of iterations.
One of the simplest Java loops to learn is the "for" loop. Initialization, test, and update expressions, which together make up its loop-control, are all gathered at the top of the
loop, inside of circular brackets ().
Switch statement
Syntax
for (initialization expression, Boolean-expression , increment/decrement) {
// statements of for loop
}
Flow Chart
// java program using for loop
public class Student { public static void main(String[] args) { int i; for(i = 1; i <= 10; i++) { System.out.println("2 * "+ i +"="+(2*i)); } } }
Output:
2 * 1=2
2 * 2=4
2 * 3=6
2 * 4=8
2 * 5=10
2 * 6=12
2 * 7=14
2 * 8=16
2 * 9=18
2 * 10=20
Let's learn about enum from below point
// java program using for loop
public class Student { public static void main(String[] args) { for(int i = 0; i < 10 ; i++) { System.out.println(i); } System.out.println( "Value of i is " +i ); //Accessing i outside for loop body gives an error } }
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
i cannot be resolved to a variable
Now variable can be accesed outside for loop
// java program using for loop
public class Student { public static void main(String[] args) { int i; for( i = 0; i < 10 ; i++) { System.out.println(i); } System.out.println( "Value of i is " +i ); //now accessing i outside for loop body will not gives error } }
Output:
0
1
2
3
4
5
6
7
8
9
Value of i is 10
// java program using for loop to show multiple initializations
public class Student { public static void main(String[] args) { int a = 1; for (int b = 0, c = 2; a < 5 && b < 5; a++, b++) { System.out.println("Value of b: "+b); } System.out.println("Value of a: "+a); } }
Output:
Value of b: 0
Value of b: 1
Value of b: 2
Value of b: 3
Value of a: 5
// Redeclaration of a variable
public class Student { public static void main(String[] args) { int a=10, b=10; for(a=1,b=0; a<5; a++,b++) { System.out.println("Value of a is: "+a); System.out.println("Value of b is: "+b); } } }
Output:
Value of a is: 1
Value of b IS : 0
Value of a is: 2
Value of b is: 1
Value of a is: 3
Value of b is: 2
Value of a is: 4
Value of b is: 3
// Redeclaration of a variable
public class Student { public static void main(String[] args) { int i = 2, sum = 0 ; for( ; i <= 20 ; sum +=i, i=2+i ) { System.out.println(i); } System.out.println("The sum of first 10 multiple of 2 is: " +sum) ; } }
Output:
2
4
6
8
10
12
14
16
18
20
The sum of first 10 multiple of 2 is: 110
// Redeclaration of a variable
public class Student { public static void main(String[] args) { for( int x = 10 ; ; x-- ) { System.out.println("This is an example of infinite for loop"); } } }
Output:
This is an example of infinite for loop
Syntax
for (data_type variable_name: array_name) {
//statements
}
// Java Loop program using for-each loop
public class Student { public static void main(String[] args) { int arr[ ] = {5, 10, 15, 20, 25}; for(int numDisplay : arr) { System.out.println(numDisplay+" "); } } }
Output:
5
10
15
20
25
Syntax
labelname:
for (initialization expression, Boolean-expression , increment/decrement) {
//statements
}
// Java Loop program using for-each loop
public class LabeledForExample { public static void main(String[] args) { xyz: //Using Label for outer and for loop for(int a = 1; a <= 3; a++) { abc: for(int b = 1; b <= 3; b++) { if(a==2 && b==2) { break xyz; } System.out.println(a+" "+b); } } } }
Output:
1 1
1 2
1 3
2 1
Post your comment